import os
from SCons.Environment import Environment
import subprocess


class TestEnvironment(Environment):
        def __init__(self, *args, **kwargs):
                super(TestEnvironment, self).__init__(*args, **kwargs)

                self.configure()

        def configure(self):
                CPPUNIT_CONFIG = 'cppunit-config'
                cppunit_flags = subprocess.check_output([CPPUNIT_CONFIG, '--libs', '--cflags'])
                env_flags = self.ParseFlags(cppunit_flags)
                self.MergeFlags(env_flags)


Import('BASE_DIR', 'TERMS_CPPPATH')

env = TestEnvironment()
env.Append(CPPPATH=TERMS_CPPPATH)
env.Append(CXXFLAGS=["-std=c++11", '-pedantic', '-W', '-Wall', '-g', '-fcolor-diagnostics'])
env.Append(LIBPATH=BASE_DIR)
env.Append(RPATH=BASE_DIR)

TEST_SOURCES = Glob('*_test.cc')
for test_source in TEST_SOURCES:
	(test_name, _) = os.path.splitext(test_source.name)
	env.Program(test_name, source=test_source, LIBS=['terms', 'cppunit'])
	test_runner = env.Command(target=test_name + '.xml', source='./' + test_name, action='$SOURCE')
	AlwaysBuild(test_runner)

SConscript(dirs='dl', exports=['BASE_DIR', 'TERMS_CPPPATH'])
